package at.ac.tuwien.mg.wh.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.constraints.Min;


@Entity
@NamedQueries({
	@NamedQuery(
			name="numberOfFreePieces", 
			query=	"SELECT i.number - SUM(iu.number) " +
					"FROM Item i LEFT JOIN i.itemUsers iu " +
					"WHERE i.catalogId= :id " +
					"GROUP BY i.catalogId "),
	@NamedQuery(
			name="bookingForItemAndUser", 
			query=	"SELECT iu " +
					"FROM Item i, IN(i.itemUsers) iu, IN(iu.user) u " +
					"WHERE i.catalogId= :id " +
					"AND u.userId = :uid " +
					"AND iu.state = 'BOOKED' ")
	})	
public class Item implements Serializable {

	@Id
	private Long catalogId;
	private String name;
	private String description;
	private String category;
	
	@Min(0)
	@Column(nullable=false)
	private Integer number = 0;
	@OneToMany(mappedBy="item" )
	private List<ItemUser> itemUsers;
	
	private static final long serialVersionUID = 1L;
	
	
	public Item() {
		super();
	}   
	
	
	
	public Item(Long catalogId, String name, String description,
			String category, Integer number) {
		super();
		this.catalogId = catalogId;
		this.name = name;
		this.description = description;
		this.category = category;
		this.number = number;
	}



	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}   
 
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}   
	public String getCategory() {
		return this.category;
	}

	public void setCategory(String category) {
		this.category = category;
	}
	public Long getCatalogId() {
		return catalogId;
	}
	public void setCatalogId(Long catalogId) {
		this.catalogId = catalogId;
	}

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}


	public List<ItemUser> getItemUsers() {
		if (itemUsers == null) {
			itemUsers = new ArrayList<ItemUser>();
        }
		return itemUsers;
	}

	public void setItemUsers(List<ItemUser> itemUsers) {
		this.itemUsers = itemUsers;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((catalogId == null) ? 0 : catalogId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Item other = (Item) obj;
		if (catalogId == null) {
			if (other.catalogId != null)
				return false;
		} else if (!catalogId.equals(other.catalogId))
			return false;
		return true;
	}



	@Override
	public String toString() {
		return "Item [catalogId=" + catalogId + ", name=" + name
				+ ", description=" + description + ", category=" + category
				+ ", number=" + number + ", itemUsers=" + itemUsers + "]";
	}




	
	
   
	
	
}
